﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
public class HeartUIDrag : MonoBehaviour {

    public Transform Pole;
    public Transform Heart;
    public Transform Feather;
    public AudioClip HangClip;

    private AudioSource audios;

    private void Awake()
    {
        //初始倾斜
        Pole.DOLocalRotate(new Vector3(0,0,10),0.75f);
        Heart.DOLocalRotate(new Vector3(0,0,-10),0.75f);
        Feather.DOLocalRotate(new Vector3(0, 0, -10), 0.75f);
    }

    // Use this for initialization
    void Start ()
    {
        audios = Camera.main.GetComponent<AudioSource>();

        DragEventListener dragListener = gameObject.AddComponent<DragEventListener>();

        dragListener.BeginDrag += delegate (GameObject obj, PointerEventData eventData)
        {
            Heart.parent = GameObject.Find("Canvas").transform;
            Feather.DOLocalRotate(new Vector3(0,0,10), 0.75f);
        };
        dragListener.Drag += delegate (GameObject obj, PointerEventData eventData)
        {
            
            Pole.DOLocalRotate(new Vector3(0,0,-10), 0.75f);

            var rt = gameObject.GetComponent<RectTransform>();

            Vector3 globalMousePos;
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out globalMousePos))
            {
                rt.position = globalMousePos;
            }
        };
        dragListener.EndDrag += delegate (GameObject obj, PointerEventData eventData)
        {
            if (CalibrationCollider.Instance.LeftCalibrationNum == 1)
            {
                this.transform.parent = GameObject.Find("L01").transform;
                this.transform.localPosition = new Vector3(0,0,0);
            }
            else if (CalibrationCollider.Instance.LeftCalibrationNum == 2)
            {
                this.transform.parent = GameObject.Find("L02").transform;
                this.transform.localPosition = new Vector3(0, 0, 0);
            }
            else if (CalibrationCollider.Instance.LeftCalibrationNum == 3)
            {
                this.transform.parent = GameObject.Find("L03").transform;
                this.transform.localPosition = new Vector3(0, 0, 0);
            }
            else if (CalibrationCollider.Instance.LeftCalibrationNum == 4)
            {
                this.transform.parent = GameObject.Find("L04").transform;
                this.transform.localPosition = new Vector3(0, 0, 0);
            }

            if (CalibrationCollider.Instance.LeftCalibrationNum>1)
            {
                Pole.DOLocalRotate(new Vector3(0,0,10), 0.75f);
                Heart.DOLocalRotate(new Vector3(0, 0, -10), 0.75f);
                Feather.DOLocalRotate(new Vector3(0, 0, -10), 0.75f);
            }
            else
            {
                Level4BeginEnd.Instance.End();
                //分数
                GameObject.Find("UI_Game").SendMessage("AddNum", 4);
                //关卡解锁
                if (!LevelSingleton.GetInstance().levelData.ContainsKey(4))
                {
                    LevelSingleton.GetInstance().levelData.Add(4, 1);
                }
                
                PlayerPrefs.SetInt("4", 1);

                audios.clip = HangClip;
                audios.Play();
                audios.loop = false;
            }
        };
    }

}
